package vrann.dataformats.json;

public abstract class JsonElement {

	public abstract String getEscapedString();
	
	public static JsonElement initFromString(String input) throws Exception
	{
		JsonElement element;
		if (input.matches("\\{.*\\}")) {
			try {
				element = JsonHash.initFromString(input);
			} catch (Exception e) {
				throw e;
			}
		} else if (input.matches("\\[.*\\]")){
			try {
				element = JsonArray.initFromString(input);
			} catch (Exception e) {
				throw e;
			}	
		} else {
			try {
				element = JsonString.initFromString(input);
			} catch (Exception e) {
				throw e;
			}	
		}
		return element;
	}
	
	protected static int countBraceOccurancies(String input, Character brace)
	{
		return input.length() - input.replaceAll("\\" + brace, "").length();
	}
}
